package com.pbs.utils;

import java.io.Reader;
import java.util.ArrayList;

import com.pbs.contact.Address;
import com.pbs.contact.Contact;
import com.pbs.contact.Email;
import com.pbs.contact.IM;
import com.pbs.contact.Organization;
import com.pbs.contact.Phone;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;


public class MyXStream {
	public static String toXML(ArrayList<Contact> list) {
		// ClientContact co chua thuoc tinh Bitmap nen khong the tao XML duoc
		// Can phai chuyen cac doi tuong ClientContact thanh cac doi tuong
		// Contact
//		ArrayList<Contact> contacts = new ArrayList<Contact>();
//
//		for (Contact c : list) {
//			Contact myContact = new Contact();
//			myContact.setAddresses(c.getAddresses());
//			myContact.setChecked(false);
//			myContact.setDisplayName(c.getDisplayName());
//			myContact.setEmail(c.getEmail());
//			myContact.setId(c.getId());
//			myContact.setImAddresses(c.getImAddresses());
//			myContact.setNotes(c.getNotes());
//			myContact.setOrganization(c.getOrganization());
//			myContact.setPhone(c.getPhone());
//			contacts.add(myContact);
//		}

		XStream writer = new XStream();
		writer.alias("contact", Contact.class);
		writer.aliasAttribute(Contact.class, "checked", "checked");
		writer.aliasAttribute(Contact.class, "id", "ID");
		writer.aliasAttribute(Contact.class, "displayName", "name");
		writer.aliasField("notes", Contact.class, "notes");
		
		writer.alias("phone", Phone.class);
		writer.aliasAttribute(Phone.class, "number", "value");
		writer.aliasAttribute(Phone.class, "type", "type");
		
		writer.alias("email", Email.class);
		writer.aliasAttribute(Email.class, "address", "value");
		writer.aliasAttribute(Email.class, "type", "type");
		
		writer.alias("address", Address.class);
		writer.aliasAttribute(Address.class, "poBox", "pobox");
		writer.aliasAttribute(Address.class, "street", "street");
		writer.aliasAttribute(Address.class, "city", "city");
		writer.aliasAttribute(Address.class, "state", "state");
		writer.aliasAttribute(Address.class, "postalCode", "postal_code");
		writer.aliasAttribute(Address.class, "country", "country");
		writer.aliasAttribute(Address.class, "type", "type");
		
		writer.alias("IM", IM.class);
		writer.aliasAttribute(IM.class, "name", "name");
		writer.aliasAttribute(IM.class, "type", "type");
		
		writer.alias("organization", Organization.class);
		writer.aliasAttribute(Organization.class, "organization", "org");
		writer.aliasAttribute(Organization.class, "positions", "pos");
		writer.aliasAttribute(Organization.class, "type", "type");

		return writer.toXML(list);
	}

	@SuppressWarnings("unchecked")
	public static ArrayList<Contact> fromXML(String xml) {
		XStream reader = new XStream(new DomDriver());
		reader.alias("contact", Contact.class);
		reader.aliasAttribute(Contact.class, "checked", "checked");
		reader.aliasAttribute(Contact.class, "id", "ID");
		reader.aliasAttribute(Contact.class, "displayName", "name");
		reader.aliasField("notes", Contact.class, "notes");
		
		reader.alias("phone", Phone.class);
		reader.aliasAttribute(Phone.class, "number", "value");
		reader.aliasAttribute(Phone.class, "type", "type");
		
		reader.alias("email", Email.class);
		reader.aliasAttribute(Email.class, "address", "value");
		reader.aliasAttribute(Email.class, "type", "type");
		
		reader.alias("address", Address.class);
		reader.aliasAttribute(Address.class, "poBox", "pobox");
		reader.aliasAttribute(Address.class, "street", "street");
		reader.aliasAttribute(Address.class, "city", "city");
		reader.aliasAttribute(Address.class, "state", "state");
		reader.aliasAttribute(Address.class, "postalCode", "postal_code");
		reader.aliasAttribute(Address.class, "country", "country");
		reader.aliasAttribute(Address.class, "type", "type");
		
		reader.alias("IM", IM.class);
		reader.aliasAttribute(IM.class, "name", "name");
		reader.aliasAttribute(IM.class, "type", "type");
		
		reader.alias("organization", Organization.class);
		reader.aliasAttribute(Organization.class, "organization", "org");
		reader.aliasAttribute(Organization.class, "positions", "pos");
		reader.aliasAttribute(Organization.class, "type", "type");

		return (ArrayList<Contact>) reader.fromXML(xml);
	}

	@SuppressWarnings("unchecked")
	public static ArrayList<Contact> fromXML(Reader xml) {
		XStream reader = new XStream(new DomDriver());
		reader.alias("contact", Contact.class);
		reader.aliasAttribute(Contact.class, "checked", "checked");
		reader.aliasAttribute(Contact.class, "id", "ID");
		reader.aliasAttribute(Contact.class, "displayName", "name");
		reader.aliasField("notes", Contact.class, "notes");
		
		reader.alias("phone", Phone.class);
		reader.aliasAttribute(Phone.class, "number", "value");
		reader.aliasAttribute(Phone.class, "type", "type");
		
		reader.alias("email", Email.class);
		reader.aliasAttribute(Email.class, "address", "value");
		reader.aliasAttribute(Email.class, "type", "type");
		
		reader.alias("address", Address.class);
		reader.aliasAttribute(Address.class, "poBox", "pobox");
		reader.aliasAttribute(Address.class, "street", "street");
		reader.aliasAttribute(Address.class, "city", "city");
		reader.aliasAttribute(Address.class, "state", "state");
		reader.aliasAttribute(Address.class, "postalCode", "postal_code");
		reader.aliasAttribute(Address.class, "country", "country");
		reader.aliasAttribute(Address.class, "type", "type");
		
		reader.alias("IM", IM.class);
		reader.aliasAttribute(IM.class, "name", "name");
		reader.aliasAttribute(IM.class, "type", "type");
		
		reader.alias("organization", Organization.class);
		reader.aliasAttribute(Organization.class, "organization", "org");
		reader.aliasAttribute(Organization.class, "positions", "pos");
		reader.aliasAttribute(Organization.class, "type", "type");

		return (ArrayList<Contact>) reader.fromXML(xml);
	}
}
